from models.friend_model import Friend  #引入数据模型
from db_config import db



# 好友数据库操作
class Friend_Operation():

    def __init__(self):
        self.__fields__ = ["id", "id1", "id2", "massage"]

    # 好友查询
    def all_friend(self, id1):
        friend_list = Friend.query.filter_by(id1 = id1)#.first()
        return friend_list

    # 添加好友 
    def add_friend(self, id1, id2):
        relationship1 = Friend(id1 = id1, id2 = id2)
        relationship2 = Friend(id1 = id2, id2 = id1)
        db.session.add(relationship1)
        db.session.add(relationship2)
        db.session.commit()
    
    # 删除好友 
    def delete_friend(self, id1, id2):
        relationship1 = Friend.query.filter_by(id1 = id1, id2 = id2).first()
        relationship2 = Friend.query.filter_by(id1 = id2, id2 = id1).first()
        # db.session.add() delete()添加的参数必须为对应的对象
        db.session.delete(relationship1)
        db.session.delete(relationship2)
        db.session.commit()
    
    # 查询好友关系 
    def check_friend(self, id1, id2):
        relationship = Friend.query.filter_by(id1 = id1, id2 = id2)
        return relationship

        


    